using System;
using System.Collections.Generic;
using System.Text;

namespace TidhiGanitam.DataModel
{
    public  class RaviRoutineItem
    {
        int m_iIndex;
        DateTime m_Date;
        Rasi m_NirayanaRaviKendram;
        Rasi m_NirayanaMadyamaRavi;
        MandaRaviItem m_MandaRaviItem;
        Rasi m_NakshatraPramanam;
        int m_iBhagam;
        int m_iSign = 1;
        public int Sign
        {
            get { return m_iSign; }
        }
        public int Bhagam
        {
            get { return m_iBhagam; }
        }
        public DateTime Date
        {
            get { return m_Date; }
        }
        public Rasi RaviKendram
        {
            get { return m_NirayanaRaviKendram; }
        }
        public Rasi MadyamaRavi
        {
            get { return m_NirayanaMadyamaRavi; }
        }
        public Rasi ChandraPhalam
        {
            get { return m_NakshatraPramanam; }
        }
        public Rasi RaviMandaPhalam
        {
            get { return m_MandaRaviItem.MandaPadakam; }
        }
        public Rasi Gati
        {
            get { return m_MandaRaviItem.Gati; }
        }
        public Degree Karnam
        {
            get { return m_MandaRaviItem.Karnam; }
        }
        public RaviRoutineItem(int Bhagam, Rasi NirayanaRaviKendram, Rasi NirayanaMadyamaRavi, DateTime Date)
        {
            m_iBhagam = Bhagam;
            m_NirayanaRaviKendram = new Rasi(NirayanaRaviKendram);
            m_NirayanaMadyamaRavi = new Rasi(NirayanaMadyamaRavi);
            m_Date = Date;
            m_iIndex = (int)(m_NirayanaRaviKendram.Rasulu * 30 + m_NirayanaRaviKendram.Bhagalu);
            //m_MandaRaviItem = (MandaRaviItem)Constants.MandaRaviPadakalu[m_iIndex];
            m_MandaRaviItem = Constants.GetMandaRaviItem(m_iIndex);
            m_NakshatraPramanam = new Rasi();
        }
        public void Calculate()
        {
            Rasi MandaPhalamDiff;
            Rasi GatiDiff;
            Degree KarnamDiff;
            Rasi MadyamaRaviKendramAdvancement = new Rasi(0, 0, m_NirayanaRaviKendram.Liptalu, m_NirayanaRaviKendram.Viliptalu);
            if (m_iIndex < 360)
            {
                //Find out the Advancements for Manda Ravi Phalam, Gati and Karnam
                //MandaRaviItem NextMandaRaviItem = (MandaRaviItem)Constants.MandaRaviPadakalu[m_iIndex + 1];
                MandaRaviItem NextMandaRaviItem = (MandaRaviItem)Constants.GetMandaRaviItem (m_iIndex + 1);
                MandaPhalamDiff = Rasi.Difference(NextMandaRaviItem.MandaPadakam,m_MandaRaviItem.MandaPadakam);
                GatiDiff = Rasi.Difference(NextMandaRaviItem.Gati, m_MandaRaviItem.Gati);
                KarnamDiff = Degree.Difference(NextMandaRaviItem.Karnam, m_MandaRaviItem.Karnam);
                if (MadyamaRaviKendramAdvancement.Liptalu > 0 || MadyamaRaviKendramAdvancement.Viliptalu > 0)
                {
                    MandaPhalamDiff = ((Rasi)(MandaPhalamDiff * MadyamaRaviKendramAdvancement)).Round();
                    GatiDiff = ((Rasi)(GatiDiff * MadyamaRaviKendramAdvancement)).Round();
                    KarnamDiff = KarnamDiff * new Degree(MadyamaRaviKendramAdvancement.Rasulu*30+ MadyamaRaviKendramAdvancement.Bhagalu,
                                MadyamaRaviKendramAdvancement.Liptalu, MadyamaRaviKendramAdvancement.Viliptalu);
                }
            }
            else
            {
                MandaPhalamDiff = new Rasi();
                GatiDiff = new Rasi();
                KarnamDiff = new Degree();
            }
            m_MandaRaviItem.MandaPadakam += MandaPhalamDiff;
            m_MandaRaviItem.Gati += GatiDiff;
            m_MandaRaviItem.Karnam += KarnamDiff;
            //If kendram is between 0-0-0-0 and 5-29-59-59 Subtract MandaRaviPhalam Difference
            //If kendram is between 6-0-0-0 and 11-29-59-59 Add MandaRaviPhalam Difference
            if (m_NirayanaRaviKendram < new Rasi(5, 29, 59, 59))
            {
                //Calculate madhyama ravi
                m_NirayanaMadyamaRavi -= m_MandaRaviItem.MandaPadakam;
                m_iSign = -1;
            }
            else
            {
                //Calculate madhyama ravi
                m_NirayanaMadyamaRavi += m_MandaRaviItem.MandaPadakam;
                m_iSign = 1;
            }
            //Find the Nakshatra Pramanam
            m_NakshatraPramanam = ((Rasi)( m_MandaRaviItem.MandaPadakam / 27)).Round();

            m_NirayanaMadyamaRavi.Refresh();
            m_NirayanaRaviKendram.Refresh();
        }


    }
}
